/*
 * Copyright (c) 2011, Yawning <yawninglol at gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   * Neither the name of the Mew Developers nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package yawning.mew.protwarrior;

import yawning.mew.ProcEffect.Trigger;
import yawning.mew.character.CharacterModel.AttackResult;
import yawning.mew.sim.SimulationAction;
import yawning.mew.sim.SimulationEventAction;

public class RendAction extends SimulationEventAction {
	ProtWarriorSimulator mState;

	private double mDamage;
	private double mPCrit;
	private double mDamageMult;

	RendAction(ProtWarriorSimulator state) {
		super(state.mSimEngine);

		mState = state;
	}

	@Override
	public double perform() {
		int cost = mState.mModel.rendCost;

		if (!mState.hasRage(cost)) return SimulationAction.ACTION_FAIL;

		AttackResult result = mState.mModel.getFrontalYellowResult();

		if (result == AttackResult.MISS) {
			mNrRendMiss++;

			if (mState.mIsLogging) mState.mLog.log("Rend: MISS\n");
		} else if (result == AttackResult.DODGE) {
			mNrRendDodge++;

			if (mState.mIsLogging) mState.mLog.log("Rend: DODGE\n");
		} else if (result == AttackResult.PARRY) {
			mNrRendParry++;

			if (mState.mIsLogging) mState.mLog.log("Rend: PARRY\n");
		} else {
			mDamage = mState.mModel.mLevelDep.getRendBaseDamage() + mState.mModel.getWeaponDamage() * mState.mModel.mLevelDep.getRendCoeff();
			double damage = mDamage;

			if (result == AttackResult.CRIT) {
				mNrRendCrit++;
				damage *= mState.mModel.multCrit;
			} else {
				mNrRendHit++;
			}


			// Snapshot the stats.
			mPCrit = Math.min(1, mState.mModel.getMeleeCritChance());
			mDamageMult = mState.mToTTEv.getMult();
			mDamageMult *= mState.mBastionOfDefenseEv.isUp() ? mState.mModel.multBastionOfDefense : 1;
			mDamageMult *= mState.mRudeInterruptionEv.isUp() ? mState.mModel.multRudeInterruption : 1;
			mDamageMult *= mState.mTier_12_Dps_2pcEv.isUp() ? mState.mModel.tier_12_dps_2pc_damageMult : 1;

			// Apply the direct damage
			damage *= mDamageMult;

			if (mState.mIsLogging) mState.mLog.log("Rend: %s Damage: %d\n", result, Math.round(damage));
			mRendDamage += Math.round(damage);

			if (!isUp()) {
				schedule(mState.mModel.rendDotDuration, mState.mModel.rendDotTickInterval);
			} else {
				extendExpiry(mState.mModel.rendDotDuration + getNextTickTime() - getExpiryTime());
			}

			if (result == AttackResult.CRIT) {
				// Does not trigger deep wounds.
				mState.mSimEngine.triggerProc(mState.mActiveTarget, Trigger.YELLOWCRIT);
			} else {
				mState.mSimEngine.triggerProc(mState.mActiveTarget, Trigger.YELLOW);
			}
		}

		mState.onRageLoss(cost);

		return 1.5;
	}

	public void snapshotAndReschedule() {
		// Snapshot the stats.
		mDamage = mState.mModel.mLevelDep.getRendBaseDamage() + mState.mModel.getWeaponDamage() * mState.mModel.mLevelDep.getRendCoeff();
		mPCrit = Math.min(1, mState.mModel.getMeleeCritChance());
		mDamageMult = mState.mToTTEv.getMult();
		mDamageMult *= mState.mBastionOfDefenseEv.isUp() ? mState.mModel.multBastionOfDefense : 1;
		mDamageMult *= mState.mRudeInterruptionEv.isUp() ? mState.mModel.multRudeInterruption : 1;

		if (!isUp()) {
			// This should never happen.
			if (mState.mIsLogging) mState.mLog.log("Rend\n");

			schedule(mState.mModel.rendDotDuration, mState.mModel.rendDotTickInterval);
		} else {
			if (mState.mIsLogging) mState.mLog.log("Rend: Refresh\n");

			extendExpiry(mState.mModel.rendDotDuration + getNextTickTime() - getExpiryTime());
		}
	}

	@Override
	public void onTick() {
		double damage = mDamage;
		AttackResult result = mState.mModel.getDotResult(mPCrit);

		if (result == AttackResult.CRIT) {
			mNrRendDoTCrit++;
			damage *= mState.mModel.multCrit;
		} else {
			mNrRendDoTHit++;
		}

		damage *= mState.getBleedDamageMult();
		damage *= mState.mModel.multThunderstruckDamage;
		damage *= mDamageMult;

		mRendDoTDamage += Math.round(damage);

		if (mState.mIsLogging) mState.mLog.log("Rend Tick: %s Damage: %d\n", result, Math.round(damage));

		if (result == AttackResult.CRIT) mState.mSimEngine.triggerProc(mState.mActiveTarget, Trigger.DOTCRIT); else mState.mSimEngine.triggerProc(mState.mActiveTarget, Trigger.DOT);
	}

	@Override
	public void onExpire() {
		if (mState.mIsLogging) mState.mLog.log("Rend: Expiration\n");
	}

	long mRendDamage = 0;
	int mNrRendMiss = 0;
	int mNrRendDodge = 0;
	int mNrRendParry = 0;
	int mNrRendCrit = 0;
	int mNrRendHit = 0;

	long mRendDoTDamage = 0;
	int mNrRendDoTCrit = 0;
	int mNrRendDoTHit = 0;
}
